package com.atguigu.schedule.controller;

import com.atguigu.schedule.common.Result;
import com.atguigu.schedule.service.YOLOApiManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * YOLO二维目标检测控制器
 * 与人脸识别系统并行工作，不修改现有功能
 */
@Slf4j
@RestController
@RequestMapping("/api/yolo-detection")
@RequiredArgsConstructor
public class YOLOObjectDetectionController {
    
    private final YOLOApiManager yoloApiManager;
    
    /**
     * 检测图像中的目标对象
     */
    @PostMapping("/detect-objects")
    public Result<List<Map<String, Object>>> detectObjects(@RequestParam("image") MultipartFile image) {
        log.info("🎯 收到YOLO检测请求，图像大小: {} bytes", image.getSize());
        
        try {
            if (image.isEmpty()) {
                log.warn("⚠️ 图像文件为空");
                return Result.build(null, 400, "图像文件不能为空");
            }
            
            byte[] imageData = image.getBytes();
            log.info("📦 图像数据大小: {} bytes", imageData.length);
            
            List<Map<String, Object>> results = yoloApiManager.detectObjects(imageData).get();
            log.info("🎯 YOLO检测完成，检测到 {} 个目标", results.size());
            
            return Result.ok(results);
            
        } catch (Exception e) {
            log.error("❌ YOLO目标检测失败", e);
            return Result.build(null, 500, "目标检测失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取YOLO模型信息
     */
    @GetMapping("/model-info")
    public Result<Map<String, Object>> getModelInfo() {
        try {
            Map<String, Object> info = yoloApiManager.getModelInfo();
            return Result.ok(info);
            
        } catch (Exception e) {
            log.error("获取YOLO模型信息失败", e);
            return Result.build(null, 500, "获取模型信息失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取检测统计信息
     */
    @GetMapping("/detection-stats")
    public Result<Map<String, Object>> getDetectionStats() {
        try {
            Map<String, Object> stats = yoloApiManager.getDetectionStats();
            return Result.ok(stats);
            
        } catch (Exception e) {
            log.error("获取检测统计失败", e);
            return Result.build(null, 500, "获取检测统计失败: " + e.getMessage());
        }
    }
    
    /**
     * 切换YOLO检测服务状态
     */
    @PostMapping("/toggle-service")
    public Result<Map<String, Object>> toggleService(@RequestBody Map<String, Object> request) {
        try {
            Boolean enabled = (Boolean) request.get("enabled");
            boolean result = yoloApiManager.toggleService(enabled);
            
            Map<String, Object> response = new HashMap<>();
            response.put("enabled", result);
            response.put("message", result ? "YOLO目标检测服务已启用" : "YOLO目标检测服务已禁用");
            
            return Result.ok(response);
            
        } catch (Exception e) {
            log.error("切换YOLO服务状态失败", e);
            return Result.build(null, 500, "切换服务状态失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取服务状态
     */
    @GetMapping("/status")
    public Result<Map<String, Object>> getServiceStatus() {
        try {
            Map<String, Object> status = yoloApiManager.getServiceStatus();
            return Result.ok(status);
            
        } catch (Exception e) {
            log.error("获取YOLO服务状态失败", e);
            return Result.build(null, 500, "获取服务状态失败: " + e.getMessage());
        }
    }
}